package com.csjbot.snowbot_rogue.services;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;

import com.csjbot.csjbase.base.CsjBaseService;
import com.csjbot.csjbase.log.Csjlogger;
import com.csjbot.snowbot_rogue.servers.serials.SnowBotSerialServer;

import java.util.List;

public class RogueBGService extends CsjBaseService {
    private SnowBotSerialServer snowBotAction = SnowBotSerialServer.getOurInstance();

    private Handler mHandler = new Handler();

    private Runnable sendRunable = new Runnable() {
        @Override
        public void run() {
//            snowBotAction.requireAllSensor();
//            mHandler.postDelayed(sendRunable, 1800000);
        }
    };

    private boolean activityIsOnTop(Context context, String activityName) {
        if (context == null || TextUtils.isEmpty(activityName)) {
            return false;
        }

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (activityName.equals(cpn.getClassName())) {
                return true;
            }
        }

        return false;
    }

//    @Override
//    public boolean useEventBus() {
//        return true;
//    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public boolean onAIUIEvent(AIUIEvent event) {
//        switch (event.getTag()) {
//            case EventsConstants.AIUIEvents.AIUI_EVENT_WAKEUP:
//                Csjlogger.debug("ismusic " + "开启SpeechActivity");
//                wakeupReceiver(this);
//                break;
//        }
//        return false;
//    }


//    private void wakeupReceiver(Context context) {
//        if (!activityIsOnTop(context, "com.csjbot.snowbot_rogue.activity.SpeechActivity")) {
//            Intent it = new Intent(context, SpeechActivity.class);
//            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(it);
//        }
//    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Csjlogger.info("穿山甲SDK版本号 : 4.0, code = 17 ；2016年11月26日");

//        mHandler.postDelayed(sendRunable, 5000);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
